package com.uxwine.alarm.mon;

import com.uxwine.alarm.Alarm;
import com.uxwine.util.Log;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;

public class AlarmReceiver extends BroadcastReceiver
{

	/**
	 * If the alarm is older than STALE_WINDOW, ignore. It is probably the
	 * result of a time or timezone change
	 */
	private final static int STALE_WINDOW = 30 * 60 * 1000;

	@Override
	public void onReceive(Context context, Intent intent)
	{
		String action = intent.getAction();
		
		//由AlarmService发送，用来在结束闹钟之后，更新通知栏的UI
		//参考: AlarmService.sendKillBroadcast
		if (AlarmUtil.ACTION_ALARM_KILLED.equals(action))
		{
			// The alarm has been killed, update the notification
			Alarm alarm = intent.getParcelableExtra(AlarmUtil.EXTRA_ALARM_INTENT);
			int timeout = intent.getIntExtra(AlarmUtil.EXTRA_ALARM_KILLED_TIMEOUT, -1);
			//new AlarmNotify(context).update(alarm, timeout);

		} 
		//由客户端程序发送，用来取消贪睡模式 
		else if (AlarmUtil.ACTION_SNOOZE_CANCEL.equals(action))
		{
			AlarmUtil.saveSnoozeAlert(context, -1, -1);
		} 
		//由系统AlarmManger发送，用来通知闹钟到点
		//参考: AlarmUtil.enableAlert
		else if (AlarmUtil.ACTION_ALARM_ALERT.equals(action))
		{
			handleAlarmAlert(context, intent);
		}
	}

	protected void handleAlarmAlert(Context context, Intent intent)
	{
		Alarm alarm = null;
		// Grab the alarm from the intent. Since the remote AlarmManagerService
		// fills in the Intent to add some extra data, it must unparcel the
		// Alarm object. It throws a ClassNotFoundException when unparcelling.
		// To avoid this, do the marshalling ourselves.
		final byte[] data = intent.getByteArrayExtra(AlarmUtil.EXTRA_ALARM_RAW);
		if (data != null)
		{
			Parcel in = Parcel.obtain();
			in.unmarshall(data, 0, data.length);
			in.setDataPosition(0);
			alarm = Alarm.CREATOR.createFromParcel(in);
		}
		
		if (alarm == null)
		{
			Log.d("zhangzhongming", "Failed to parse the alarm from the intent");
			
			// Make sure we set the next alert if needed.
			AlarmUtil.setNextAlert(context);
			return;
		}

		// Disable the snooze alert if this alarm is the snooze.
		AlarmUtil.disableSnoozeAlert(context, alarm.id);
		
		
		if (alarm.cycle.isRepeatSet())
		{
			// Enable the next alert if there is one. The above call to
			// enableAlarm will call setNextAlert so avoid calling it twice.
			AlarmUtil.setNextAlert(context);
		} 
		else
		{
			// Disable this alarm if it does not repeat.
			AlarmUtil.enableAlarm(context, alarm.id, false);
		}

		// Intentionally verbose: always log the alarm time to provide useful
		// information in bug reports.
		long now = System.currentTimeMillis();

		// Always verbose to track down time change problems.
		if (now > alarm.trigger + STALE_WINDOW)
		{
			Log.d("zhangzhongming", "Ignoring stale alarm");
			return;
		}

		// Maintain a cpu wake lock until the AlarmActivity and AlarmService
		//can pick it up.
		AlarmWakeLock.getInstance(context).acquire();

		/* Close dialogs and window shade */
		Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
		context.sendBroadcast(closeDialogs);

		// Play the alarm alert and vibrate the device.
		Intent playAlarm = new Intent(AlarmUtil.ACTION_ALARM_ALERT);
		playAlarm.putExtra(AlarmUtil.EXTRA_ALARM_INTENT, alarm);
		context.startService(playAlarm);

		//show the notification
		//new AlarmNotify(context).notify(alarm);
		
		new AlarmNotify(context).updateStated(alarm, 0);
	}
}
